package swordoffer.offer29;

public class Debug {
    public static void main(String[] args) {
        int[][] nums = new int[][]{{}};
        spiralOrder(nums);
    }
    public static int[] spiralOrder(int[][] matrix) {
        if(matrix.length == 0){
            return new int[0];
        }
        int left = 0;
        int right = matrix[0].length - 1;

        int top = 0;
        int bottom = matrix.length - 1;
        int[] res = new int[(bottom + 1) * (right + 1)];
        int x = 0;
        while (true){
            //从左到右
            for(int i = left; i <= right; i++){
                res[x++] = matrix[top][i];
            }
            //从左到右遍历完
            if(++top > bottom){
                break;
            }
            //从上到下
            for (int i = top; i <= bottom; i++){
                res[x++] = matrix[i][right];
            }
            //从上到下遍历完
            if(--right < left){
                break;
            }
            //从右到左
            for (int i = right; i >= left; i--){
                res[x++] = matrix[bottom][i];
            }
            //从右到左遍历完
            if(--bottom < top){
                break;
            }
            //从下到上
            for (int i = bottom; i >= top; i--){
                res[x++] = matrix[i][left];
            }
            if(++left > right){
                break;
            }
        }
        return res;
    }
}
